﻿using System;
using System.ComponentModel;
using System.IO;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShopB2C.Helper {
    /// <summary>
    /// 页码生成控件，一直显示10个号码
    /// </summary>
    public class PagerHelper : CompositeControl {
        private int _pageCount;//总页数
        private string _hrefFormat;//导航到链接格式化字符串
        private int _pageIndex;//当前页
        private string _lastPageText, _firstPageText, _pageUpText, _pageDownText;
        /// <summary>
        /// 最后一页需要显示的文本,默认为末页
        /// </summary>
        [Description("最后一页需要显示的文本,默认为末页")]
        public string LastPageText {
            get { return _lastPageText; }
            set { _lastPageText = value; }
        }
        /// <summary>
        /// 第一页需要显示的文本，默认为首页
        /// </summary>
        [Description("第一页需要显示的文本，默认为首页")]
        public string FirstPageText {
            get { return _firstPageText; }
            set { _firstPageText = value; }
        }
        /// <summary>
        /// 上一页需要显示的文本，默认为上一页
        /// </summary>
        [Description("上一页需要显示的文本，默认为上一页")]
        public string PageUpText {
            get { return _pageUpText; }
            set { _pageUpText = value; }
        }
        /// <summary>
        /// 下一页需要显示的文本，默认为下一页
        /// </summary>
        [Description("下一页需要显示的文本，默认为下一页")]
        public string PageDownText {
            get { return _pageDownText; }
            set { _pageDownText = value; }
        }
        /// <summary>
        /// 当前页索引,默认为1
        /// </summary>
        [Description("当前页索引,默认为1")]
        public int PageIndex {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }
        /// <summary>
        /// 导航到格式化字符串,需要占用字符串格式化{0}号展位符
        /// </summary>
        [Description("导航到链接字符串,需要占用字符串格式化{0}号展位符")]
        public string HrefFormat {
            get { return _hrefFormat; }
            set { _hrefFormat = value; }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        [Description("总页数")]
        public int PageCount {
            get { return _pageCount; }
            set { _pageCount = value; }
        }
        /// <summary>
        /// 默认呈现标记
        /// </summary>
        protected override HtmlTextWriterTag TagKey {
            get {
                return HtmlTextWriterTag.Span;
            }
        }
        /// <summary>
        /// 创建子控件
        /// </summary>
        protected override void CreateChildControls() {
            Controls.Clear();
            // if (_pageCount > 1)
            // {
            //如果当前页码小于0或者大于总页数设置当前页码
            _pageIndex = _pageIndex > _pageCount ? _pageCount : _pageIndex;
            _pageIndex = _pageIndex < 1 ? 1 : _pageIndex;

            int starNumber = 1, endNumber = _pageCount;
            //一直显示10个号码
            if (_pageIndex > 4) {
                endNumber = _pageIndex + 5 < _pageCount ? _pageIndex + 5 : _pageCount;
            }
            else {
                endNumber = _pageCount > 10 ? 10 : _pageCount;
            }

            starNumber = endNumber - 9 > 0 ? endNumber - 9 : 1;

            //第一页
            HyperLink firstPage = new HyperLink();
            firstPage.Text = _firstPageText ?? "首页";
            firstPage.NavigateUrl = _pageIndex > 1 ? string.Format(_hrefFormat, 1) : null;
            if (_pageIndex <= 1) {
                firstPage.Attributes.Add("class", "disabled");
                firstPage.Attributes.CssStyle.Add(HtmlTextWriterStyle.MarginLeft, "0px");
            }
            this.Controls.Add(firstPage);

            //上一页
            //if (_pageIndex > 1)
            //{
            HyperLink previousPage = new HyperLink();
            previousPage.Text = _pageUpText ?? "上一页";
            previousPage.NavigateUrl = _pageIndex > 1 ? string.Format(_hrefFormat, _pageIndex - 1) : null;
            if (_pageIndex <= 1)
                previousPage.Attributes.Add("class", "disabled");
            this.Controls.Add(previousPage);
            //}
            //开始编号
            for (int i = starNumber; i <= endNumber; i++) {
                HyperLink h = new HyperLink();
                h.Text = string.Format("{0}", i);
                h.NavigateUrl = string.Format(_hrefFormat, i);
                // 当前页
                if (_pageIndex == i) {
                    h.Attributes.Add("class", "current-index");
                    h.NavigateUrl = null;
                }
                this.Controls.Add(h);
            }
            //下一页
            //if (_pageIndex < _pageCount)
            //{
            HyperLink nextPage = new HyperLink();
            nextPage.Text = _pageDownText ?? "下一页";
            nextPage.NavigateUrl = _pageIndex < _pageCount ? string.Format(_hrefFormat, _pageIndex + 1) : null;
            if (_pageIndex >= _pageCount)
                nextPage.Attributes.Add("class", "disabled");
            this.Controls.Add(nextPage);
            //}
            //最后一页
            HyperLink lastPage = new HyperLink();
            lastPage.Text = _lastPageText ?? "末页";
            lastPage.NavigateUrl = _pageIndex < _pageCount ? string.Format(_hrefFormat, _pageCount) : null;
            if (_pageIndex >= _pageCount) {
                lastPage.Attributes.Add("class", "disabled");
                lastPage.Attributes.CssStyle.Add(HtmlTextWriterStyle.MarginRight, "0px");
            }
            this.Controls.Add(lastPage);

            // }
            ChildControlsCreated = true;
        }
        /// <summary>
        /// 生成可以再客户端打印的Html
        /// </summary>
        /// <returns>可以再浏览器显示的Html代码</returns>
        public string CreateHtml() {
            string html = string.Empty;
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            this.CreateChildControls();
            this.Render(hw);
            hw.Flush();
            hw.Close();
            html = sw.ToString();
            sw.Flush();
            sw.Close();
            return html;
        }
        /// <summary>
        /// 克隆当前控件的所有属性
        /// </summary>
        public void Clone(PagerHelper pagerHelper) {
            if (pagerHelper != null) {
                this._pageCount = pagerHelper._pageCount;
                this._pageIndex = pagerHelper._pageIndex;
                this._hrefFormat = pagerHelper._hrefFormat;
                this._firstPageText = pagerHelper._firstPageText;
                this._lastPageText = pagerHelper._lastPageText;
                this._pageDownText = pagerHelper._pageDownText;
                this._pageUpText = pagerHelper._pageUpText;
            }
        }
        /// <summary>
        /// 克隆当前控件的所有属性, 为后台调用准备, 勿删除
        /// </summary>
        public void Clone(object pagerEntity) {
            RouteValueDictionary propertys = new RouteValueDictionary(pagerEntity);
            this._pageIndex = propertys["PageIndex"] != null ? Convert.ToInt32(propertys["PageIndex"]) : 0;
            this._pageCount = propertys["PageCount"] != null ? Convert.ToInt32(propertys["PageCount"]) : 0;
            this._firstPageText = propertys["FirstPageText"] != null ? propertys["FirstPageText"].ToString() : null;
            this._lastPageText = propertys["LastPageText"] != null ? propertys["LastPageText"].ToString() : null;
            this._pageUpText = propertys["PageUpText"] != null ? propertys["PageUpText"].ToString() : null;
            this._pageDownText = propertys["PageDownText"] != null ? propertys["PageDownText"].ToString() : null;
            this._hrefFormat = propertys["HrefFormat"] != null ? propertys["HrefFormat"].ToString() : null;
        }
    }

    #region MvcHtmlHelper扩展方法:页码显示

    public static class MvcHtmlHelper {
        /// <summary>
        /// 页码显示
        /// </summary>
        /// <param name="html">Html</param>
        /// <param name="pageIndex">当前索引</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="hrefFormat">链接到字符串，格式为pageIndex={0},占用格式化字符串{0}，请注意字符转意</param>
        /// <returns>可以在浏览器打印的Html</returns>
        public static string Pager(this HtmlHelper html, int pageIndex, int pageCount, string hrefFormat) {
            return Pager(html, pageIndex, pageCount, hrefFormat, null, null);
        }
        /// <summary>
        /// 页码显示
        /// </summary>
        /// <param name="html">Html</param>
        /// <param name="pageIndex">当前索引</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="hrefFormat">链接到字符串，格式为pageIndex={0},占用格式化字符串{0}，请注意字符转意</param>
        /// <param name="firstPageText">第一页显示的文本，默认为首页</param>
        /// <param name="lastPageText">最后一页显示的文本，默认为末页</param>
        /// <returns>可以在浏览器打印的Html</returns>
        public static string Pager(this HtmlHelper html, int pageIndex, int pageCount, string hrefFormat, string firstPageText, string lastPageText) {
            return Pager(html, pageIndex, pageCount, hrefFormat, firstPageText, lastPageText, null, null);
        }
        /// <summary>
        /// 页码显示
        /// </summary>
        /// <param name="html">Html</param>
        /// <param name="pageIndex">当前索引</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="hrefFormat">链接到字符串，格式为pageIndex={0},占用格式化字符串{0}，请注意字符转意</param>
        /// <param name="firstPageText">第一页显示的文本，默认为首页</param>
        /// <param name="lastPageText">最后一页显示的文本，默认为末页</param>
        /// <param name="pageUpText">上一页显示的文本，默认为上一页</param>
        /// <param name="pageDownText">下一页显示的文本，默认为下一页</param>
        /// <returns>可以在浏览器打印的Html</returns>
        public static string Pager(this HtmlHelper html, int pageIndex, int pageCount, string hrefFormat, string firstPageText, string lastPageText, string pageUpText, string pageDownText) {
            return Pager(html, new {
                PageIndex = pageIndex,
                PageCount = pageCount,
                HrefFormat = hrefFormat,
                FirstPageText = firstPageText,
                LastPageText = lastPageText,
                PageUpText = pageUpText,
                PageDownText = pageDownText

            });
        }
        /// <summary>
        /// 页码显示
        /// </summary>
        /// <param name="html">Html</param>
        /// <param name="pagerEntity">页码属性匿名类型，其中可以用的字符串为:PageIndex:当前索引,PageCount:总页数
        /// HrefFormat:链接到格式化字符串，占用{格式化字符串{0}占位符,请注意字符转义,FirstPageText,LastPageText,PageUpText,PageDownText</param>
        /// <returns>可以在浏览器打印的Html</returns>
        public static string Pager(this HtmlHelper html, object pagerEntity) {
            RouteValueDictionary propertys = new RouteValueDictionary(pagerEntity);

            return Pager(html, new PagerHelper() {
                PageIndex = propertys["PageIndex"] != null ? Convert.ToInt32(propertys["PageIndex"]) : 0,
                PageCount = propertys["PageCount"] != null ? Convert.ToInt32(propertys["PageCount"]) : 0,

                FirstPageText = propertys["FirstPageText"] != null ? propertys["FirstPageText"].ToString() : null,
                LastPageText = propertys["LastPageText"] != null ? propertys["LastPageText"].ToString() : null,

                PageUpText = propertys["PageUpText"] != null ? propertys["PageUpText"].ToString() : null,
                PageDownText = propertys["PageDownText"] != null ? propertys["PageDownText"].ToString() : null,

                HrefFormat = propertys["HrefFormat"] != null ? propertys["HrefFormat"].ToString() : null,
            });

        }
        /// <summary>
        /// 页码显示
        /// </summary>
        /// <param name="html">Html</param>
        /// <param name="pageHelper">PagerHelper实体</param>
        /// <returns>可以在浏览器打印的Html</returns>
        public static string Pager(this HtmlHelper html, PagerHelper pageHelper) {
            PagerHelper helper = new PagerHelper();
            helper.Clone(pageHelper);
            return helper.CreateHtml();
        }

    }

    #endregion
}